﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Rhino.Mocks;
using WroxGallery.Actions;
using WroxGallery.Controllers;
using WroxGallery.Models;
using WroxGallery.Models.Interfaces;

namespace WroxGallery.Tests.Actions
{
    /// <summary>
    /// Summary description for PhotoGalleriesFilterAttributeTest
    /// </summary>
    [TestClass]
    public class PhotoAlbumsFilterAttributeTest
    {

        private MockRepository mocks;
        private IGalleryRepository galleryRepository;
        private IConfigurationRepository configurationRepository;

        [TestInitialize]
        public void SetupController()
        {
            mocks = new MockRepository();
            galleryRepository = mocks.DynamicMock<IGalleryRepository>();
            configurationRepository = mocks.DynamicMock<IConfigurationRepository>();
        }


        [TestMethod]
        public void PhotoAlbumsFilter_Adds_List_Of_Galleries_To_ViewData()
        {
            using (mocks.Record())
            {

                Expect.Call(galleryRepository.GetAlbums(5)).Return(BuildFakeAlbumList(5));
            }

            HomeControllerForTesting controller = new HomeControllerForTesting(configurationRepository, galleryRepository);
            ViewDataDictionary viewData = new ViewDataDictionary();
            ActionExecutedContext context = ActionFiltersTestingHelper.GetActionExecutedContext(viewData);

            using (mocks.Playback())
            {
                controller.OnActionExecuted(context);
                Assert.IsNotNull(viewData["Albums"] as AlbumCollection);
            }

        }

        private AlbumCollection BuildFakeAlbumList(int size)
        {
            AlbumCollection collection = new AlbumCollection();
            for (int i = 0; i < size; i++)
            {
                collection.Add(new Album());
            }
            return collection;
        }
    }

    internal class HomeControllerForTesting : HomeController
    {
        public HomeControllerForTesting(IConfigurationRepository repository, IGalleryRepository galleryRepository): base(repository,galleryRepository)
        {
            
        }

        public new void OnActionExecuted(ActionExecutedContext context)
        {
            base.OnActionExecuted(context);
        }
    }
}
